package com.cherry.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.*;

@RestController
public class MetricCollectorController {

    @GetMapping("/get")
    public String get() {
        return "success";
    }

    @GetMapping("/queryDb")
    public String query() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://8.140.200.108:3306/tmall2", "cherry", "cherry@A408.");
        PreparedStatement preparedStatement = connection.prepareStatement("select * from user");
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String name = resultSet.getString("user_name");
            System.out.println(name);
        }
        return "success";
    }

    @RequestMapping("/test2")
    @ResponseBody
    public String testcase() throws IOException {
        // Like gateway forward trace header.
        URL url = new URL("http://localhost:8080/get");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.addRequestProperty("key", "value");
        connection.addRequestProperty("sw8", "123456");
        int responseCode = connection.getResponseCode();
        return "Success:" + responseCode;
    }

}
